import { login, adminLogin, logout, getInfo } from '@/api/user'
import { getNum } from '@/api/record'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  uid: null,
  name: '',
  role: '',
  avatar: '',
  token: getToken(),
  notification: 0,
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ID: (state, uid) => {
    state.uid = uid
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLE: (state, role) => {
    state.role = role;
  },
  SET_NUM: (state, num) => {
    state.notification = num;
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { uid, psw, role } = userInfo
    return new Promise((resolve, reject) => {
      login({ uid: uid.trim(), password: psw, role:role }).then(response => {
        const { load } = response
        commit('SET_TOKEN', load)
        setToken(load)
        // console.log(load,"at store.user.js")
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  adminLogin({ commit }, userInfo) {
    const { uid, psw, role } = userInfo
    return new Promise((resolve, reject) => {
      adminLogin({ uid: uid.trim(), password: psw, role:role }).then(response => {
        const { load } = response
        commit('SET_TOKEN', load)
        setToken(load)
        // console.log(load,"at store.user.js")
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const load = response.load
        if (!load) {
          reject('Verification failed, please Login again.')
        }
        if (!load.role ) {
          reject('getInfo: role must be a word! this for devoloper,delete this')
        }
        commit('SET_ID', load.uid)
        commit('SET_ROLE', load.role)
        commit('SET_NAME', load.name)
        commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
        resolve(load)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // get Admin notifications
  getNum({ commit,}) {
    if(state.role == "Admin"){
      getNum().then(response => {
        const { load } = response
        commit('SET_NUM', load)
      }).catch(error => {
        console.log(error)
      })
    }
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLE', '')
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

